using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class ShoppingCarts
	{
		public int GetMaxId()
		{
			return Conn.GetMaxID("CartID", "HLShop_ShoppingCarts");
		}
		public bool Exists(int CartID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HLShop_ShoppingCarts");
			stringBuilder.Append(" where CartID=@CartID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CartID", SqlDbType.Int, 4)
			};
			array[0].Value = CartID;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.ShoppingCarts model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HLShop_ShoppingCarts(");
			stringBuilder.Append("userID,productID,productStocks,productPrice,buyproductNum,productMoney,InputTime,UpdateTime)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@userID,@productID,@productStocks,@productPrice,@buyproductNum,@productMoney,@InputTime,@UpdateTime)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@userID", SqlDbType.Int, 4),
				new SqlParameter("@productID", SqlDbType.Int, 4),
				new SqlParameter("@productStocks", SqlDbType.Int, 4),
				new SqlParameter("@productPrice", SqlDbType.Decimal, 9),
				new SqlParameter("@buyproductNum", SqlDbType.Int, 4),
				new SqlParameter("@productMoney", SqlDbType.Decimal, 9),
				new SqlParameter("@InputTime", SqlDbType.DateTime),
				new SqlParameter("@UpdateTime", SqlDbType.DateTime)
			};
			array[0].Value = model.userID;
			array[1].Value = model.productID;
			array[2].Value = model.productStocks;
			array[3].Value = model.productPrice;
			array[4].Value = model.buyproductNum;
			array[5].Value = model.productMoney;
			array[6].Value = model.InputTime;
			array[7].Value = model.UpdateTime;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.ShoppingCarts model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HLShop_ShoppingCarts set ");
			stringBuilder.Append("userID=@userID,");
			stringBuilder.Append("productID=@productID,");
			stringBuilder.Append("productStocks=@productStocks,");
			stringBuilder.Append("productPrice=@productPrice,");
			stringBuilder.Append("buyproductNum=@buyproductNum,");
			stringBuilder.Append("productMoney=@productMoney,");
			stringBuilder.Append("InputTime=@InputTime,");
			stringBuilder.Append("UpdateTime=@UpdateTime");
			stringBuilder.Append(" where CartID=@CartID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@userID", SqlDbType.Int, 4),
				new SqlParameter("@productID", SqlDbType.Int, 4),
				new SqlParameter("@productStocks", SqlDbType.Int, 4),
				new SqlParameter("@productPrice", SqlDbType.Decimal, 9),
				new SqlParameter("@buyproductNum", SqlDbType.Int, 4),
				new SqlParameter("@productMoney", SqlDbType.Decimal, 9),
				new SqlParameter("@InputTime", SqlDbType.DateTime),
				new SqlParameter("@UpdateTime", SqlDbType.DateTime),
				new SqlParameter("@CartID", SqlDbType.Int, 4)
			};
			array[0].Value = model.userID;
			array[1].Value = model.productID;
			array[2].Value = model.productStocks;
			array[3].Value = model.productPrice;
			array[4].Value = model.buyproductNum;
			array[5].Value = model.productMoney;
			array[6].Value = model.InputTime;
			array[7].Value = model.UpdateTime;
			array[8].Value = model.CartID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int CartID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HLShop_ShoppingCarts ");
			stringBuilder.Append(" where CartID=@CartID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CartID", SqlDbType.Int, 4)
			};
			array[0].Value = CartID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string CartIDlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HLShop_ShoppingCarts ");
			stringBuilder.Append(" where CartID in (" + CartIDlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.ShoppingCarts GetModel(int CartID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 * from HLShop_ShoppingCarts ");
			stringBuilder.Append(" where CartID=@CartID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CartID", SqlDbType.Int, 4)
			};
			array[0].Value = CartID;
			HKB.Data.Model.ShoppingCarts shoppingCarts = new HKB.Data.Model.ShoppingCarts();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.ShoppingCarts result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.ShoppingCarts DataRowToModel(DataRow row)
		{
			HKB.Data.Model.ShoppingCarts shoppingCarts = new HKB.Data.Model.ShoppingCarts();
			if (row != null)
			{
				if (row["CartID"] != null && row["CartID"].ToString() != "")
				{
					shoppingCarts.CartID = int.Parse(row["CartID"].ToString());
				}
				if (row["userID"] != null && row["userID"].ToString() != "")
				{
					shoppingCarts.userID = new int?(int.Parse(row["userID"].ToString()));
				}
				if (row["productID"] != null && row["productID"].ToString() != "")
				{
					shoppingCarts.productID = new int?(int.Parse(row["productID"].ToString()));
				}
				if (row["productStocks"] != null && row["productStocks"].ToString() != "")
				{
					shoppingCarts.productStocks = new int?(int.Parse(row["productStocks"].ToString()));
				}
				if (row["productPrice"] != null && row["productPrice"].ToString() != "")
				{
					shoppingCarts.productPrice = new decimal?(decimal.Parse(row["productPrice"].ToString()));
				}
				if (row["buyproductNum"] != null && row["buyproductNum"].ToString() != "")
				{
					shoppingCarts.buyproductNum = new int?(int.Parse(row["buyproductNum"].ToString()));
				}
				if (row["productMoney"] != null && row["productMoney"].ToString() != "")
				{
					shoppingCarts.productMoney = new decimal?(decimal.Parse(row["productMoney"].ToString()));
				}
				if (row["InputTime"] != null && row["InputTime"].ToString() != "")
				{
					shoppingCarts.InputTime = new System.DateTime?(System.DateTime.Parse(row["InputTime"].ToString()));
				}
				if (row["UpdateTime"] != null && row["UpdateTime"].ToString() != "")
				{
					shoppingCarts.UpdateTime = new System.DateTime?(System.DateTime.Parse(row["UpdateTime"].ToString()));
				}
			}
			return shoppingCarts;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * FROM HLShop_ShoppingCarts ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * FROM HLShop_ShoppingCarts ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HLShop_ShoppingCarts ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.CartID desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HLShop_ShoppingCarts T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
	}
}
